<template>
  <div>
    <ul>
      <li :style="{ opacity }">欢迎学习Vue</li>
      <li>news001<input type="text" /></li>
      <li>news002<input type="text" /></li>
      <li>news003<input type="text" /></li>
    </ul>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "News",
  data() {
    return {
      opacity: 1,
    };
  },
  // mounted() {
  //   this.timer = setInterval(() => {
  //     console.log("定时器在执行");
  //     this.opacity -= 0.01;
  //     if (this.opacity <= 0) {
  //       this.opacity = 1;
  //     }
  //   }, 16);
  // },
  // beforeDestroy() {
  //   console.log("NEWS组件即将被销毁");
  //   clearInterval(this.timer);
  // },

  //Vue另外的两个生命周期钩子，是Router路由独有的

  //组件是激活状态时触发
  activated() {
    console.log("NEWS组件被激活了");
    this.timer = setInterval(() => {
      console.log("定时器在执行");
      this.opacity -= 0.01;
      if (this.opacity <= 0) {
        this.opacity = 1;
      }
    }, 16);
  },

  //组件是非激活状态（失活）时触发
  deactivated() {
    console.log("NEWS组件失活了");
    clearInterval(this.timer);
  },
};
</script>
